<?php


namespace app\model;

use think\Model;

class DeptModel extends Model
{
    protected $name = 'base_dept';
    protected $pk = 'dept_id';

    public function getTopDept($dept_id=0){
        $dept = $this->field("dept_id,dept_name,parent_id,dept_full_name")->find($dept_id);
        if($dept->parent_id==0){
            return $dept;
        }else{
            return $this->getTopDept($dept->parent_id);
        }
    }

    public function getAllDept($dept_id=0,$separator,$lst=[]){
        $dept = $this->field("dept_id,dept_name,parent_id")->find($dept_id);
        if($lst==[]){
            $lst = [$dept->dept_name];
        }
        else{
            $lst[] = $dept->dept_name;
        }
        if($dept->parent_id>0){
            return $this->getAllDept($dept->parent_id,$separator,$lst);
        }
        if($separator){
            return implode($separator,array_reverse($lst));
        }else{
            return $lst;
        }
    }
}